<?php

class Request {

    public $url;
    public $controller;
    public $action;
    public $params;

    public function __construct($url) {
        $this->url = $url;

        $params = Router::to_params($url); //Logger::debug(print_r($params, true));

        if (empty($params['controller'])) {
            throw new Exception('empty controller in request: ' . $url);
        }

        // ok, now we need to load ApplicationController class
        if (mini_include('ApplicationController', APP_CONTROLLERS.DS.'application_controller.php') === false) {
            throw new Exception('class Application Controller not found!');
        }

        $controllerName = $params['controller']."Controller";
        // convert to underscorelized file name
        $controllerFileName = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $controllerName)) .'.php';
        if (mini_include($controllerName, APP_CONTROLLERS.DS.$controllerFileName) === true) {

            // initilize the controller
            $this->controller = new $controllerName;
            unset($params['controller']);

            // default action is index
            $this->action = empty($params['action']) ? 'index' : $params['action'];
            unset($params['action']);

            $this->params = $params;
            return;
        }

        // static file
        // we let the response do the include
    }
}